package com.eshopkeeper;

import com.eshopkeeper.core.util.yunzhi.StringUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class LDAPJDBCUtil {

	/**
	 * 一个非常标准的连接Oracle数据库的示例代码
	 */
	public void testOracle() {
		Connection con = null;// 创建一个数据库连接
		PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement
		ResultSet result = null;// 创建一个结果集对象
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
			System.out.println("开始尝试连接数据库！");
			String url = "jdbc:oracle:thin:@222.73.156.45:61012/kfdb";
			String user = "blsc";
			String password = "blsckf";
			con = DriverManager.getConnection(url, user, password);// 获取连接
			System.out.println("连接成功！");
			String sql = "select name,nvl(value,'') value,nvl(commet,'') commet,nvl(html_info,'') as htmlinfo from system_setting";
			pre = con.prepareStatement(sql);// 实例化预编译语句
			result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数
			StringBuffer sb = new StringBuffer("version: 1\n");
			while (result.next()){
				// 当结果集不为空时
				/**
				version: 1

				dn: systemSettingName=extract.fixed.fee.max,ou=systemSetting,ou=blsc,dc=exam
				 ple,dc=com
				objectClass: systemSetting
				objectClass: top
				systemSettingName: extract.fixed.fee.max
				systemSettingValue: 0
				systemSettingCommet: 提现固定手续费(最大值)
				systemSettingHtmlInfo: 
				
				 */
				sb.append("dn: systemSettingName="+result.getString("NAME")+",ou=systemSetting,ou=blsc,dc=example,dc=com\n");
				sb.append("objectClass: systemSetting\n");
				sb.append("objectClass: top\n");
				sb.append("systemSettingName: "+result.getString("NAME")+"\n");
				if(StringUtil.isNotEmpty(result.getString("VALUE"))){
					sb.append("systemSettingValue: "+result.getString("VALUE")+"\n");
				}
				if(StringUtil.isNotEmpty(result.getString("COMMET"))){
					sb.append("systemSettingCommet: "+result.getString("COMMET")+"\n");
				}
				if(StringUtil.isNotEmpty(result.getString("HTMLINFO"))){
					sb.append("systemSettingHtmlInfo: "+result.getString("HTMLINFO")+"\n");
				}
				sb.append("\n");
			}
			System.out.println(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 逐一将上面的几个对象关闭，因为不关闭的话会影响性能、并且占用资源
				// 注意关闭的顺序，最后使用的最先关闭
				if (result != null)
					result.close();
				if (pre != null)
					pre.close();
				if (con != null)
					con.close();
				System.out.println("数据库连接已关闭！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) {
		LDAPJDBCUtil jDBCUtil= new LDAPJDBCUtil();
		jDBCUtil.testOracle();
	}
}
